﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceProcess;
using System.Configuration;
using Citi.SecurityHelper;

namespace Citi.Scheduler.WebUi.Models
{
    public class MachineCurrentExecutionTextModel
    {
        public string Hostname { get; set; }
        public DateTime Date { get; set; }
        public string MachineFullName { get; set; }
        public string Description { get; set; }
        public IEnumerable<ServiceStatusModel> Services { get; set; }
    }

    public class ServiceStatusModel
    {
        private static string NetworkUser { get { return ConfigurationManager.AppSettings["NetworkUser"]; } }
        private static string NetworkPassword { get { return ConfigurationManager.AppSettings["NetWorkPassword"]; } }

        public string ServiceName { get; set; }
        public string MachineName { get; set; }
        public string StatusText { get { return Status.GetDescription(); } }

        public bool Running { get { return Status == ServiceControllerStatus.Running; } }

        public ServiceControllerStatus Status
        {
            get
            {
                using (ImpersonateCitiUser.GetImpersonateUser(NetworkUser, NetworkUser, NetworkPassword))
                {
                    var service = new ServiceController(ServiceName, MachineName);
                    return service.Status;
                }
            }
        }
    }
}